from chardet.universaldetector import UniversalDetector

'''by：shenhuawade
qq: 317909531
功能：自动判断txt的编码格式，都自动转换成utf8编码

readtextMain(filename)
------------------------------------------
输入参数filename: 文件名 '游记：皮尔森啤酒博物馆.txt'
-------------------------------------------
输出参数list: 每行文字组成的列表

------------------------------------------
code/time:2019.1.5
'''


def readText(file,encode_info):
    # 2.按行读取
    textlist =[]
    mylen = 0
    for line in open(file, errors='ignore',encoding=encode_info):
        # 清洗空格后如果不为空就下一步处理
        if line.strip() != '':
            # print(f"本行内容为{line}")
            # 每行的数据都需要清洗空格后进行下一步处理
            abc = line.strip()
            mylen = mylen+len(abc)
            print(mylen)
            if mylen <= 9900:
                textlist.append(abc)
            else:
                break
        else:
            # print("本行为空")
            pass
    print(mylen)
    return textlist




def get_encode_info(file):
    with open(file, 'rb') as f:
        detector = UniversalDetector()

        for line in f.readlines():
            # print(line)
            detector.feed(line)
            if detector.done:
                break
        detector.close()
        # print(detector.result['encoding'])
        if detector.result['encoding'] == None:
            return 'ascii'
        return detector.result['encoding']


def read_file(file):
    with open(file, 'rb') as f:
        return f.read()


def write_file(content, file):
    with open(file, 'wb') as f:
        f.write(content)


def convert_encode2utf8(file, original_encode, des_encode):
    file_content = read_file(file)
    print(file_content)
    file_decode = file_content.decode(original_encode, 'ignore')
    file_encode = file_decode.encode(des_encode)
    write_file(file_encode, file)



#核心函数
def readtextMain(filename):
    filename = rf'{filename}'
    file_content = read_file(filename)
    encode_info = get_encode_info(filename)
    if encode_info != 'utf-8':
        convert_encode2utf8(filename, encode_info, 'utf-8')
    encode_info = get_encode_info(filename)
    print(encode_info)
    textlist = readText(filename, encode_info)
    return textlist




if __name__ == "__main__":

    filename = r'123.txt'
    textlist = readtextMain(filename)
    print(textlist)

